package com.boot2.core.web.filter;

import com.boot2.core.web.RequestContext;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhangweilin
 * @date 2018/6/13 8:27
 * @description: 最基础过滤器
 */
@Data
@CommonsLog
public class SysFilter extends OncePerRequestFilter {


    @SneakyThrows
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        //初始化整站上下文
        RequestContext.start(request, response);

//        ModifiableHeaderHttpServletRequest request1 = new ModifiableHeaderHttpServletRequest(request);
        try {
            filterChain.doFilter(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

        RequestContext requestContext = RequestContext.get();
        if (null != requestContext) {
            RequestContext.get().clearContext();
        }
    }

}